<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <operFormComponent ref="operFormComponentRef" :id="id" @success="success" :operStr="operStr" />
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { AssetsFixedApi, AssetsFixedVO } from '@/api/school/assets/fixed'
import { UserVO } from '@/api/system/user'
import { propTypes } from '@/utils/propTypes'
import operFormComponent from './operFormComponent.vue'
const route = useRoute()

/** 固定资产 表单 */
defineOptions({ name: 'AssetsFixedForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const operFormComponentRef = ref()
const formLoading = computed(() => !!operFormComponentRef?.value?.formLoading)

const formRules = reactive({
  name: [{ required: true, message: '物资名称不能为空', trigger: 'blur' }],
  price: [{ required: true, message: '维修费用不能为空', trigger: 'blur' }],
  inRemark: [{ required: true, message: '备注不能为空', trigger: 'blur' }],
  userIds: [{ required: true, message: '领用人不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref
const selectedUsers = ref<UserVO[]>([])
const userSelectFormRef = ref()

/** 处理用户选择确认 */
const handleUserSelectConfirm = (_, users: UserVO[]) => {
  selectedUsers.value = users || []
}

/** 打开人员选择 */
const openUserSelect = () => {
  userSelectFormRef.value.open(0, selectedUsers.value)
}

/** 移除人员 */
const handleRemoveUser = (user: UserVO) => {
  selectedUsers.value = selectedUsers.value.filter((userItem: any) => userItem.id !== user.id)
}

//窗口标题
const operMap = {
  borrow: '物资领用',
  returns: '归还物资',
  fix: '物资报修',
  scrap: '物资报废',
  reputin: '重新入库'
}
const id: any = ref('')
const operStr: any = ref('')
/** 打开弹窗 */
const open = async (row: AssetsFixedVO, oper: string) => {
  dialogVisible.value = true
  dialogTitle.value = operMap[oper] || '未知操作'
  id.value = row.id
  operStr.value = oper
}
const success = () => {
  dialogVisible.value = false
  // 发送操作成功的事件
  emit('success')
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  operFormComponentRef.value.submitForm()
}
</script>

<style scoped>
:deep(.blue-divider .el-divider__text) {
  color: rgb(64, 158, 255);
}
</style>
